package com.man;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.std.Announce;
import com.std.DB;

public class AnnounceCtrl {
	private DB db;
	public AnnounceCtrl(){
		db=new DB();
	}
	public void close() {
		db.close();
	}
	public void insertItem(String username,String title,String content) {
		String sql="insert into announce_t values(announce_s.nextval,?,?,?,current_date)";
		Object []params={username,title,content};
		db.execute(sql, params);
	}
	public List<Announce> getItems() {
		String sql="select * from announce_t";
		db.execute(sql, null);
		List<Announce> announceList=new ArrayList<Announce>();
		ResultSet rs=db.getResultset();
		try {
			while(rs.next()){
				Announce announce=new Announce();
				announce.setContent(rs.getString("content"));
				announce.setAnnounceTime(rs.getDate("announce_time"));
				announce.setTitle(rs.getString("title"));
				announce.setAnnounceId(rs.getInt("announce_id"));
				announce.setUsername(rs.getString("username"));
				announceList.add(announce);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		return announceList;
	}
	public Announce getItem(int announceId) {
		String sql="select * from announce_t where announce_id=?";
		Object []params={announceId};
		db.execute(sql, params);
		ResultSet rs=db.getResultset();
		try {
			if(rs.next()){
				Announce announce=new Announce();
				announce.setContent(rs.getString("content"));
				announce.setAnnounceTime(rs.getDate("announce_time"));
				announce.setTitle(rs.getString("title"));
				announce.setAnnounceId(rs.getInt("announce_id"));
				announce.setUsername(rs.getString("username"));
				return announce;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	public void deleteItem(int announceId) {
		String sql="delete from announce_t where announce_id=?";
		Object []params={announceId};
		db.execute(sql, params);
	}
}
